﻿using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.UI.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace classTest
{
    public partial class AttributeTableWindow : Window
    {

        private FeatureQueryResult queryResults;
        private MapView mapView;
        private FeatureLayer aLayer;
        private int index = 0;

        private List<Feature> FeatureQuery { set; get; }

        public AttributeTableWindow(FeatureLayer layer, MapView MyMapView,List<Feature> FeatureQuery1)
        {
            InitializeComponent();
            aLayer = layer;
            mapView = MyMapView;
            FeatureQuery = FeatureQuery1;
            Initialize();
        }


        private async void Initialize()
        {
            int index = 0;
            for(int i=0;i<aLayer.FeatureTable.Fields.Count;i++)
            {
                Field f = aLayer.FeatureTable.Fields.ElementAt(i);
                ComboBoxItem a = new ComboBoxItem();
                a.Content = f.Name;
                if (f.Name.ToLower() == "name") index = i;
                comboBoxRecord.Items.Add(a);
            }
            label.Content = aLayer.Name;
            QueryParameters pQueryPara = new QueryParameters();
            queryResults = await aLayer.FeatureTable.QueryFeaturesAsync(pQueryPara);
            comboBoxRecord.SelectedIndex = index;
        }


        // 为AttributeTableWindow窗体中记录集组合框的选择事件添加处理代码
        private void comboBoxRecord_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (comboBoxRecord.SelectedIndex != -1)
            {
                string key = (comboBoxRecord.SelectedItem as ComboBoxItem).Content.ToString();
                List<string> va = new List<string>();
                List<string> vc = new List<string>();
                
                List<Feature> fc = queryResults.ToList();
                vc = fc[0].Attributes.Keys.ToList();
                for(int j = 0; j < vc.Count(); j++)
                {
                    if (vc[j].ToString() == key)
                    {
                        index = j;
                        break;
                    }
                }
                foreach (Feature i in queryResults)
                {
                    //可能值为空，所以得加异常处理
                    try
                    {
                        va.Add(i.Attributes.ElementAt(index).Value.ToString());
                    }
                    catch
                    {
                        va.Add("空");
                    }
                    
                }
                comboBoxvalue.ItemsSource = va;
                comboBoxvalue.SelectedIndex = 0;
            }
        }


        private async void ComboBoxvalue_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (comboBoxvalue.SelectedIndex!=-1)
            {
                Feature ft = queryResults.ElementAt(comboBoxvalue.SelectedIndex);//获取索引所在要素
                try
                {
                    aLayer.ClearSelection();
                    aLayer.SelectFeature(ft);
                }
                catch
                {

                }

                dataGridContent.AutoGenerateColumns = true;//数据格网对象自动生成列
                dataGridContent.ItemsSource = ft.Attributes;//设置数据格网对象中项的数据源
                if (FeatureQuery != null && FeatureQuery.Count > 0) FeatureQuery.Clear();
                FeatureQuery.Add(ft);

                if(ft.Geometry.GeometryType == GeometryType.Point)
                {
                    await mapView.SetViewpointCenterAsync(ft.Geometry as MapPoint, 1520);
                }
                else
                {
                    await mapView.SetViewpointGeometryAsync(ft.Geometry.Extent, 10);
                }
                
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            try
            {
                if(FeatureQuery.Count>0) FeatureQuery.Clear();
                aLayer.ClearSelection();
            }
            catch
            {

            }

        }

    }
}
